﻿Imports System.Data.SqlClient
Public Class frmEditDependant
    Public DepID As Integer = -1
    Public EmpName, Position, Branch, Empno As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Sub LoadDependent()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim cmd As New SqlCommand("select * from Dependent  where Sno = " & DepID, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtDependent.Text = Reader.Item("DependentName")
                Me.CombGender.Text = Reader.Item("Gender")
                Me.ComboRelationship.Text = Reader.Item("Relationship")
                Me.DTBirth.Value = Reader.Item("DoB")
                Me.chkPassport.Checked = Reader.Item("PassHolder")
                Me.txtPassportNo.Text = Reader.Item("PassportNo")
                Me.txtPssprtIsuPlce.Text = Reader.Item("IssuePlace")
                Me.DTExpireDate.Value = Reader.Item("ExpDate")
            End While

            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If DepID <> -1 Then
                Dim cmd As New SqlCommand("Update Dependent set DependentName=N'" & Me.txtDependent.Text & "', Relationship=N'" & Me.ComboRelationship.Text & _
                                        "', DoB=N'" & Me.DTBirth.Value.ToShortDateString & "'," & _
                                        "PassHolder='" & Me.chkPassport.Checked & "', PassportNo=N'" & Me.txtPassportNo.Text & _
                                        "' , ExpDate=N'" & Me.DTExpireDate.Value.ToShortDateString & "', IssuePlace=N'" & Me.txtPssprtIsuPlce.Text & _
                                        "',IssueDate=N'" & DTPassprtIssue.Value & "', Gender=N'" & Me.CombGender.Text & "' where Sno=" & DepID, cnn)

                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()
                MsgBox("Saved Succesfuly!")
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmEditDependant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDependent()
    End Sub
End Class